package com.qfedu.department.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.activiti.engine.RuntimeService;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.qfedu.common.entity.WoPage;
import com.qfedu.common.entity.WoResultCode;
import com.qfedu.common.exception.handler.WoControllerExceptionHandler;
import com.qfedu.department.dto.QfLeaveDto;
import com.qfedu.department.service.QfLeaveService;
import com.qfedu.psys.dto.MenuDto;
import com.qfedu.psys.service.CoreService;
import com.qfedu.psys.util.PSysConstant;
import com.qfedu.psys.util.PSysUtil;

@Controller
@SessionAttributes(names = PSysConstant.SESSION_USER)
@RequestMapping("/department/leave/me")
public class MyLeaveController {

	/**
	 * 注入MenuService
	 */
	@Resource
	private CoreService coreService;

	/**
	 * 注入异常处理器.
	 */
	@Resource
	private WoControllerExceptionHandler exceptionHandler;

	@GetMapping("")
	String toMain(Map<String, Object> map) {
		return exceptionHandler.toLoginView(() -> {
			List<MenuDto> menus = coreService.getMenus(PSysUtil.getCurrentUser(map), "menu-department",
					"menu-myLeaves");
			map.put("menus", menus);
		}, map, "department/qfLeave-me");
	}

	/**
	 * 前端加载整个请假管理页面后,会发送AJAX请求加载请假列表数据.
	 * 
	 * @param searchContent
	 * @param page
	 * @param map
	 * @return
	 */
	@RequestMapping("/list")
	public String list(String searchContent, @RequestParam(defaultValue = "1") Long page, Map<String, Object> map) {
		return this.exceptionHandler.toAjaxErrorView(() -> {
			map.put("pageData",
					this.qfLeaveService.getMyPageData(PSysUtil.getCurrentUser(map), searchContent, page, WoPage.SIZE));
		}, map, "department/qfLeave-me-list");
	}

	/**
	 * @return
	 */
	@GetMapping("/apply")
	String apply() {
		return "department/qfLeave-apply";
	}

	/**
	 * 注入QfLeaveService.
	 */
	@Resource
	private QfLeaveService qfLeaveService;

	@PostMapping("/apply")
	@ResponseBody
	WoResultCode apply(QfLeaveDto dto, Map<String, Object> map) {
		return exceptionHandler.toJsonView(() -> qfLeaveService.apply(PSysUtil.getCurrentUser(map), dto), "创建请假成功！");
	}
	
	/**
	 * 加载请假处理表单.
	 * @param id
	 * @param map
	 * @return
	 */
	@GetMapping("/process")
	public String process(String id, Map<String, Object> map) {
		return this.exceptionHandler.toAjaxErrorView(() -> {
			map.put("leaveData", qfLeaveService.getProcessDataById(PSysUtil.getCurrentUser(map), id));
		}, map, "department/qfLeave-me-process");
	}
	
	@PostMapping("/manager")
	@ResponseBody
	WoResultCode manager(QfLeaveDto dto, Map<String, Object> map) {
		return exceptionHandler.toJsonView(() -> qfLeaveService.manager(PSysUtil.getCurrentUser(map), dto), "部门审批请假成功！");
	}

	@Resource
	private RuntimeService runtimeService;
	
	@RequestMapping("/diagram/{id}")
	ResponseEntity<byte[]> loadDiagram(@PathVariable("id") String id) {
		return this.qfLeaveService.getDiagram (id);
	}
}
